package cn.sumpu.app.coolsk.dao.impl;

import java.util.*;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.*;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.Message;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class MessageDaoImpl extends SqlSessionDaoSupport implements MessageDao {
	private final static Logger logger = LoggerFactory
			.getLogger(MessageDaoImpl.class);

	//批量发送站内信
	@Override
	public void addMessages(Map<String,Object> map) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MessageMapper.addMessages", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addMessages resource failure.", e);
			}
			throw new AppBusinessException("addMessages resource failure.", e);
		}
	}
	//删除站内信
	@Override
	public void delMessage(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.MessageMapper.delMessage", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delMessage resource failure.", e);
			}
			throw new AppBusinessException("delMessage resource failure.", e);
		}
	}
	//查询站内信
	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessages(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.MessageMapper.getMessages", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessages resource failure.", e);
			}
			throw new AppBusinessException("getMessages resource failure.", e);
		}
	}
	//查询站内信总条数
	@Override
	public Integer getMessageCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.MessageMapper.getMessageCount", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageCount resource failure.", e);
			}
			throw new AppBusinessException("getMessageCount resource failure.", e);
		}
	}
	
}
